<template>
	<view class="addressList">
		<view v-if="dataList.length" class="listBox">
			<view class="listIitem" v-for="(item,index) in dataList">
				<view class="itemName_moblie" @click="handleSelect(item)">
					<view class="name">
						{{item.name}}
					</view>
					<view class="moblie">
						{{item.phone}}
					</view>
				</view>

				<view class="address" @click="handleSelect(item)">
					{{item.area}} {{item.address}}
				</view>
				<view class="heng"></view>


				<view class="handleBox">
					<view class="leftBox" @click="handledefault(item)">
						<image v-if="item.type=='1'" src="../../static/image/a1.png"
							style="width: 24rpx;height: 24rpx;margin-right: 10rpx;" mode=""></image>
						<image v-else src="../../static/image/a2.png"
							style="width: 24rpx;height: 24rpx;margin-right: 10rpx;" mode=""></image>
						<view class="">
							默认地址
						</view>
					</view>

					<view class="rightBox">
						<view class="rightitem" @click="handleEditaddress(item)">
							<image src="../../static/image/bianji.png"
								style="width: 24rpx;height: 24rpx;margin-right: 10rpx;" mode="">
							</image>
							<view class="">
								编辑
							</view>
						</view>
						<view class="rightitem" @click="handleDelAddress(item)">
							<image src="../../static/image/shanchu.png"
								style="width: 24rpx;height: 24rpx;margin-right: 10rpx;" mode="">
							</image>
							<view class="">
								删除
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-else class="" style="margin-top: 50rpx;">
			<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂无地址数据">
			</u-empty>
		</view>

		<view class="" style="height: 180rpx;"></view>

		<view class="btnbottom">
			<view class="btn" @click="handleAdd">
				新增地址
			</view>
		</view>

		<u-modal :show="delshow" :title="title" :showCancelButton="true" @confirm="handleconfirmdel"
			@cancel="delshow = false" confirmColor="#439B05"></u-modal>

	</view>
</template>

<script>
	import {
		getuserareaList,
		setuserarea,
		deluserarea
	} from "@/api/api.js"
	export default {
		data() {
			return {
				dataList: [],
				title: "确定要删除该地址吗?",
				delshow: false,
				delId: null,
				jumptype: false,
			}
		},
		onShow() {
			this.handlegetuserareaList()
		},
		onLoad(options) {
			// console.log("444", options)
			if (options.type == '1') {
				this.jumptype = true
			}
		},
		methods: {
			async handlegetuserareaList() {
				let res = await getuserareaList()
				this.dataList = res.data
			},

			//设置取消默认地址
			async handledefault(item) {
				console.log("rr", item.id)
				let res = await setuserarea({
					id: item.id
				})
				if (res.code == 1000) {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					});
					this.handlegetuserareaList()
				} else {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					});
				}
			},

			handleDelAddress(item) {
				this.delshow = true
				this.delId = item.id
			},

			async handleconfirmdel() {
				let res = await deluserarea({
					id: this.delId
				})
				if (res.code == 1000) {
					this.delshow = false
					this.handlegetuserareaList()
				}
				uni.showToast({
					title: res.message,
					icon: "none",
					duration: 2000
				});
			},

			handleAdd() {
				uni.navigateTo({
					url: "./addoreditAddress"
				})
			},
			handleSelect(item) {
				if (this.jumptype) {
					uni.setStorageSync("selectAddress", item)
					uni.navigateBack({
						delta: 1
					})
				}
			},
			handleEditaddress(item) {
				uni.navigateTo({
					url: `./addoreditAddress?id=${item.id}`
				})
			},

		}
	}
</script>

<style scoped lang="scss">
	.btnbottom {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		background-color: #ffffff;
		position: fixed;
		bottom: 0;

		.btn {
			width: 670rpx;
			height: 80rpx;
			background: #439B05;
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 80rpx;
			text-align: center;
			border-radius: 100rpx;
			margin: 0 auto;
		}
	}

	.addressList {
		.listIitem {
			width: 100%;
			height: 270rpx;
			background-color: #ffffff;
			padding: 30rpx;
			box-sizing: border-box;
			margin-top: 20rpx;

			.itemName_moblie {

				display: flex;
				width: 100%;
				justify-content: space-between;

				.name {
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #333333;
					line-height: 33rpx;
				}

				.moblie {
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #333333;
					line-height: 33rpx;
				}
			}

			.address {
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 28rpx;
				margin-top: 30rpx;
			}

			.heng {
				width: 100%;
				height: 2rpx;
				background-color: #F5F5F5;
				margin-top: 60rpx;
			}

			.handleBox {
				width: 100%;
				display: flex;
				justify-content: space-between;
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 28rpx;
				margin-top: 30rpx;

				.leftBox {
					display: flex;
					align-items: center;
				}

				.rightBox {
					display: flex;
					align-items: center;

					.rightitem {
						display: flex;
						align-items: center;
						margin-left: 30rpx;
					}
				}
			}
		}
	}
</style>